MACRO(ANDROID_SETUP_TOOLCHAIN)
	SET(ONE_VALUE_ARGS CPU_ARCH PLATFORM ANDROID_NDK_PATH OUTPUT_DIR)
	CMAKE_PARSE_ARGUMENTS(ANDROID_SETUP_TOOLCHAIN "${OPTIONS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN})
	IF(ANDROID_SETUP_TOOLCHAIN_UNPARSED_ARGUMENTS)
		MESSAGE(FATAL_ERROR "unexpected arguments: ${ANDROID_SETUP_TOOLCHAIN_UNPARSED_ARGUMENTS}")
	ENDIF()

	IF(NOT ANDROID_SETUP_TOOLCHAIN_OUTPUT_DIR)
		MESSAGE(FATAL_ERROR "you must specify an output directory for the modified Android NDK")
	ENDIF()

	IF(NOT ANDROID_SETUP_TOOLCHAIN_ANDROID_NDK_PATH)
		MESSAGE(FATAL_ERROR "you must specify the path to Android NDK")
	ENDIF()

	IF(ANDROID_SETUP_TOOLCHAIN_CPU_ARCH STREQUAL "x86")
		SET(_ANDROID_BIN_PREFIX "i686-android-linux")
		SET(ANDROID_ARCH "x86")
		SET(ANDROID_ARCH_SHORT "x86")
		SET(ANDROID_ARCH_LIBS "x86")
		SET(ANDROID_ARCH_FLAGS "")
		SET(ANDROID_ARCH_LDFLAGS "")
	ELSEIF(ANDROID_SETUP_TOOLCHAIN_CPU_ARCH STREQUAL "armv7-a")
		SET(_ANDROID_BIN_PREFIX "arm-linux-androideabi")
		SET(ANDROID_ARCH "arm-linux-androideabi")
		SET(ANDROID_ARCH_SHORT "arm")
		SET(ANDROID_ARCH_LIBS "armeabi-v7a")
		SET(ANDROID_ARCH_CFLAGS "-march=armv7-a -mfloat-abi=softfp -mfpu=neon")
		SET(ANDROID_ARCH_LDFLAGS "-Wl,--fix-cortex-a8")
	ELSEIF(ANDROID_SETUP_TOOLCHAIN_CPU_ARCH STREQUAL "arm")
		SET(_ANDROID_BIN_PREFIX "arm-linux-androideabi")
		SET(ANDROID_ARCH "arm-linux-androideabi")
		SET(ANDROID_ARCH_SHORT "arm")
		SET(ANDROID_ARCH_LIBS "armeabi")
		SET(ANDROID_ARCH_FLAGS "")
		SET(ANDROID_ARCH_LDFLAGS "")
	ELSEIF(ANDROID_SETUP_TOOLCHAIN_CPU_ARCH)
		MESSAGE(FATAL_ERROR "Unsupported architecture \"${ANDROID_SETUP_TOOLCHAIN_CPU_ARCH}\". The build system currently supports the following toolchains: x86, arm-linux-androideabi")
	ELSE()
		MESSAGE(FATAL_ERROR "You must specify an architecture by using the CPU_ARCH option")
	ENDIF()

	SET(CMAKE_SYSTEM_NAME Android)
	SET(CMAKE_SYSTEM_VERSION 1)

	FILE(GLOB _TOOLCHAIN_DIRS ${ANDROID_SETUP_TOOLCHAIN_ANDROID_NDK_PATH}/toolchains/${ANDROID_ARCH}-*)
	FOREACH(_TOOLCHAIN_DIR ${_TOOLCHAIN_DIRS})
		STRING(REGEX MATCH "${ANDROID_SETUP_TOOLCHAIN_ANDROID_NDK_PATH}/toolchains/${ANDROID_ARCH}-([0-9.]+)" _TOOLCHAIN_DIR_RESULT ${_TOOLCHAIN_DIR})
		IF(_TOOLCHAIN_DIR_RESULT AND ANDROID_TOOLCHAIN_VERSION VERSION_LESS CMAKE_MATCH_1)
			SET(_ANDROID_TOOLCHAIN_VERSION ${CMAKE_MATCH_1})
			SET(_ANDROID_TOOLCHAIN_DIR ${_TOOLCHAIN_DIR})
		ENDIF()
	ENDFOREACH()

	IF(NOT _ANDROID_TOOLCHAIN_DIR)
		MESSAGE(FATAL_ERROR "The specified toolchain is not available")
	ENDIF()

	IF(WIN32)
		SET(_CURRENT_PLATFORM "windows")
	ELSEIF(APPLE)
		SET(_CURRENT_PLATFORM "darwin-x86")
	ELSE(UNIX)
		SET(_CURRENT_PLATFORM "linux-x86")
	ENDIF()

	SET(_ANDROID_PREBUILT_DIR ${_ANDROID_TOOLCHAIN_DIR}/prebuilt/${_CURRENT_PLATFORM})
	message(${_ANDROID_PREBUILT_DIR})
	IF(NOT EXISTS ${_ANDROID_PREBUILT_DIR})
		MESSAGE(FATAL_ERROR "Prebuilt binaries are not available for the current platform: ${_CURRENT_PLATFORM}")
	ENDIF()

	FILE(GLOB _PLATFORM_DIRS ${ANDROID_SETUP_TOOLCHAIN_ANDROID_NDK_PATH}/platforms/android-*)
	IF(NOT ANDROID_SETUP_TOOLCHAIN_PLATFORM)
		FOREACH(_PLATFORM_DIR ${_PLATFORM_DIRS})
			STRING(REGEX MATCH "${ANDROID_SETUP_TOOLCHAIN_ANDROID_NDK_PATH}/platforms/android-([0-9]+)" _PLATFORM_DIR_RESULT ${_PLATFORM_DIR})
			IF(_PLATFORM_DIR_RESULT AND _ANDROID_PLATFORM_VERSION VERSION_LESS CMAKE_MATCH_1)
				SET(_ANDROID_PLATFORM_VERSION ${CMAKE_MATCH_1})
				SET(ANDROID_PLATFORM_DIR ${_PLATFORM_DIR}/arch-${ANDROID_ARCH_SHORT})
			ENDIF()
		ENDFOREACH()

		IF(NOT ANDROID_PLATFORM_DIR)
			MESSAGE(FATAL_ERROR "There are not any platform files for arch-${ANDROID_ARCH_SHORT}")
		ENDIF()
	ELSE()
		SET(ANDROID_PLATFORM_DIR ${ANDROID_SETUP_TOOLCHAIN_ANDROID_NDK_PATH}/platforms/android-${ANDROID_SETUP_TOOLCHAIN_PLATFORM})
		IF(NOT EXISTS ${ANDROID_PLATFORM_DIR})
			MESSAGE(FATAL_ERROR "the requested platform is not available: android-${ANDROID_SETUP_TOOLCHAIN_PLATFORM}")
		ENDIF()
	ENDIF()

	FILE(GLOB PREBUILT_TOOLCHAIN_FILES ${_ANDROID_PREBUILT_DIR}/*)
	FILE(COPY ${PREBUILT_TOOLCHAIN_FILES} DESTINATION ${ANDROID_SETUP_TOOLCHAIN_OUTPUT_DIR})
	FILE(GLOB ANDROID_PLATFORM_FILES ${ANDROID_PLATFORM_DIR}/*)
	FILE(COPY ${ANDROID_PLATFORM_FILES} DESTINATION ${ANDROID_SETUP_TOOLCHAIN_OUTPUT_DIR})
	FILE(GLOB GNU_LIBSTDCXX_FILES ${ANDROID_TOOLCHAIN_ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/${_CURRENT_PLATFORM}/*)
	FILE(COPY ${GNU_LIBSTDCXX_FILES} DESTINATION ${ANDROID_SETUP_TOOLCHAIN_OUTPUT_DIR})

	SET(CMAKE_C_COMPILER "${ANDROID_SETUP_TOOLCHAIN_OUTPUT_DIR}/bin/${_ANDROID_BIN_PREFIX}-gcc")
	SET(CMAKE_C_FLAGS "--sysroot ${ANDROID_SETUP_TOOLCHAIN_OUTPUT_DIR}")
	SET(CMAKE_CXX_COMPILER "${ANDROID_SETUP_TOOLCHAIN_OUTPUT_DIR}/bin/${_ANDROID_BIN_PREFIX}-g++")
	SET(CMAKE_CXX_FLAGS "--sysroot ${ANDROID_SETUP_TOOLCHAIN_OUTPUT_DIR} ${ANDROID_ARCH_CFLAGS}")
	SET(CMAKE_EXE_LINKER_FLAGS "-lsupc++ -lstdc++ ${ANDROID_ARCH_LDFLAGS}")

	SET(CMAKE_FIND_ROOT_PATH ${ANDROID_SETUP_TOOLCHAIN_OUTPUT_DIR})

	SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
	SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
	SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

	SET(ANDROID_TARGET ${_ANDROID_BIN_PREFIX})
ENDMACRO()